﻿using AutoMapper.Configuration.Conventions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using VM.Common.EntityEnum;
using VM.Common.Result;
using VM.Common.strFormatUntil;
using VM.Entity.EntityDto.ShopCar;
using VM.Entity.EntityMap.DataBase;
using VM.Entity.EntityMap.Goods;
using VM.Entity.EntityMap.Lunbo;
using VM.Entity.EntityMap.ShopCar;
using VM.Entity.EntityMap.System.Code;
using VM.Entity.EntityMap.Topic;
using VM.IBLL.IShopCar;
using VM.IBLL.System.Code;
using VM.IBLL.Topic;
using VM.WebCore.Jwt;
using VM.WebCore.SwaggerExtend;
using VM.Wechat;

namespace VM.WebAPI.Controllers
{

    /// <summary>
    /// 购物车相关接口
    /// </summary>
    [Route("api/shopCar")]
    [ApiController]
    public class ShopCarController : ControllerBase
    {
        private readonly ICodeHelperBLL _ICodeHelperBLL;
        private readonly IShopCarBLL _IShopCarBLL;
        private readonly IJWTService _IJWTService;
        private readonly IHttpContextAccessor _HttpContextAccessor;

        public ShopCarController(IJWTService iJWTService, ICodeHelperBLL iCodeHelperBLL, IHttpContextAccessor httpContextAccessor, IShopCarBLL iShopCarBLL)
        {

            _ICodeHelperBLL = iCodeHelperBLL;
            _IJWTService = iJWTService;
            _HttpContextAccessor = httpContextAccessor;
            _IShopCarBLL = iShopCarBLL;
        }
        /// <summary>
        /// 添加购物车
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Authorize]//要求通过授权才行---只要能鉴权成功，都能通过] PC
        [Route("addShopCar")]
        [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
        public ApiResult addShopCar(addshopCarDto obj)
        { 
            //解析Jwt,获取到UserId
            int UserId= _IJWTService.GetUserIdByAuthInfoStr();
            //解析并优化类的传参
            obj= _ICodeHelperBLL.perfectClass<addshopCarDto>(obj,UserId,ProTypeEnum.save);
            return _IShopCarBLL.addShopCar(obj);
        }

        /// <summary>
        /// 获取我的购物车列表，   PHONE
        /// </summary>
        /// <param name="pageIndex">下标</param>
        /// <param name="pageSize">大小</param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]//要求通过授权才行---只要能鉴权成功，都能通过]
        [Route("getMyShopCarList")]
        [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
        public ApiDataResult<PagingData<getShopCarListDto>> getShopCarList(int pageIndex, int pageSize)
        {


           //解析Jwt,获取到UserId
            int UserId = _IJWTService.GetUserIdByAuthInfoStr();
            ApiDataResult<PagingData<getShopCarListDto>> Res = _IShopCarBLL.getShopCarList(UserId,pageIndex, pageSize);
            return Res;
        }

        /// <summary>
        /// 根据购物车内码删除购物车记录
        /// </summary>
        /// <param name="shopCarId">购物车列表的id</param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]//要求通过授权才行---只要能鉴权成功，都能通过]
        [Route("deleteShopCarById")]
        [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
        public ApiResult deleteShopCarById(string  shopCarIds)
        {  
            //解析Jwt,获取到UserId
            int UserId = _IJWTService.GetUserIdByAuthInfoStr();
            StrFormat sf = new StrFormat();
            int Count = sf.GetCount(",",shopCarIds)+1;
            int shopCarId = 0;
            int DelCount = 0;
            for (int i = 0; i < Count; i++) {
                shopCarId = Convert.ToInt32( sf.GetSplitStr(shopCarIds,',',i));
                ApiResult Res = _IShopCarBLL.deleteShopCarById(UserId, shopCarId);
                if (Res.code==200)
                {
                    DelCount++;
                }
            }
            ApiResult endRes = new ApiResult()
            {
                code = 200,
                msg = $@"成功删除{DelCount}条数据"


            };
            return endRes;
            
             
        }



    }
}
